package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult getPage(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.getPage(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(sd -> sd.setSetmealId(setmeal.getId()));
        setmealDishMapper.insertSetmealDishBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateSetmael(setmeal);

        //删除setmealDishes
        setmealDishMapper.deleteBatch(setmeal.getId());
        List<SetmealDish> sdList = dto.getSetmealDishes();
        sdList.forEach(sd -> sd.setSetmealId(setmeal.getId()));
        //插入setmealDishes
        setmealDishMapper.insertSetmealDishBatch(sdList);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO vo = new SetmealVO();
        Setmeal setmeal = setmealMapper.getOne(id);
        BeanUtils.copyProperties(setmeal, vo);
        List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
        vo.setSetmealDishes(list);
        return vo;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        if (status == StatusConstant.ENABLE) {
            List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
            List<Long> ids = list.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
            int i = dishMapper.countByStatus(ids);
            if (i != ids.size()) throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status).build();
        setmealMapper.updateSetmael(setmeal);
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        int i = setmealMapper.countByIds(ids);
        if (i > 0) throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        setmealMapper.deleteSetmealBatch(ids);
        ids.forEach(id -> setmealDishMapper.deleteBatch(id));
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
